#check windows version
$OsInfo = systeminfo | where{$_ -match "OS.+Microsoft Windows"}
$OsInfo
if ([String]::IsNullOrEmpty($OsInfo) -or $OsInfo -notmatch "OS.+Microsoft Windows 10")
{
    "This script must run in win10"
    pause
    exit
}
"check windows version finish"

#Install vscode
<#
.Synopsis
   Get installed software list by retrieving registry.
.DESCRIPTION
   The function return a installed software list by retrieving registry from below path;
   1.'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
   2.'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall'
   3.'HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
   Author: Mosser Lee (http://www.pstips.net/author/mosser/)
.EXAMPLE
   Get-InstalledSoftwares
.EXAMPLE
   Get-InstalledSoftwares  | Group-Object Publisher
#>
function Get-InstalledSoftwares
{
    # Read registry key as product entity.
    function ConvertTo-ProductEntity
    {
        param([Microsoft.Win32.RegistryKey]$RegKey)
        $product = '' | select Name,Publisher,Version
        $product.Name =  $_.GetValue("DisplayName")
        $product.Publisher = $_.GetValue("Publisher")
        $product.Version =  $_.GetValue("DisplayVersion")
        if( -not [string]::IsNullOrEmpty($product.Name))
        {
            $product
        }
    }
    $UninstallPaths = @(,
    # For local machine.
    'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
    # For current user.
    'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall')

    # For 32bit softwares that were installed on 64bit operating system.
    if([Environment]::Is64BitOperatingSystem) 
    {
        $UninstallPaths += 'HKLM:SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
    }
    $UninstallPaths | foreach {
        Get-ChildItem $_ | foreach {
            ConvertTo-ProductEntity -RegKey $_
        }
    }
}
$VscodeVer = Get-InstalledSoftwares | where{$_.Name -match "Microsoft Visual Studio Code.+"}
$VscodeVer
if ([String]::IsNullOrEmpty($VscodeVer))
{
    $DownloadPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
    $Des = "$DownloadPath\VSCodeUserSetup-x64-1.43.0.exe"
    $HaveShortcut = Test-Path $Des
    if ($HaveShortcut -like "False")
    {
        $Src = "https://vscode.cdn.azure.cn/stable/78a4c91400152c0f27ba4d363eb56d2835f9903a/VSCodeUserSetup-x64-1.43.0.exe"
        Invoke-WebRequest -uri $Src -OutFile $Des
    }
    Start-Process -FilePath $Des
}
"Install vscode finish"